<?php

namespace app\controllers;

use app\models\LoginFormFactory;
use myVendor\Factorys;
use srvs\ContentDetailService;
use srvs\ContentService;
use srvs\MenuService;
use srvs\UserService;
use srvs\ParamService;
use yii\base\ErrorException;
use yii\web\NotFoundHttpException;
use yii\web\UploadedFile;

class AdminController extends BackendController
{
    public $layout = "admin";

    public function actionTest(){
        return $this->render('test');
    }

    public function actionMenuEdit()
    {
        $MenuService=new MenuService();
        $menu_list=$MenuService->getAll([],["menu_sort asc"]);
        $children_menu_list=$root_meun_list=[];
        foreach($menu_list as $menu){
            if(!empty($menu["parent_id"])){
                $children_menu_list[$menu["parent_id"]][]=$menu;
            }
            else{
                $root_meun_list[]=$menu;
            }
        }

        $menu_id=I("menu_id");
        if(empty($menu_id)){
            $menu_id=current($root_meun_list)["objectId"];
        }
        $cur_menu=$MenuService->getOneById($menu_id);

        $userservice=\myVendor\Factorys::getUserService();
        $login_user_name=$userservice->getLoginUser();

        $userservice=new UserService();
        $cur_user=$userservice->getOne(["username"=>$login_user_name]);

        \Yii::$app->view->params['left_menu_active_id']=$cur_menu["objectId"];
        \Yii::$app->view->params['top_menu_active_id']="0";
        return $this->render('menu-edit',["root_menu_list"=>$root_meun_list,"children_menu_list"=>$children_menu_list,"cur_menu"=>$cur_menu,"cur_user"=>$cur_user]);
    }

    //栏目设置-删除前校验
    public function actionCheckDelete(){
        $menu_id=I("menu_id");

        $ContentService=new ContentService();
        $count=$ContentService->getCount(["menu_id"=>$menu_id]);
        if(!empty($count)){
            return $this->response(0,"该栏目下存在文章，不允许删除!");
        }
        $MenuService=new MenuService();
        $menu_count=$MenuService->getCount(["parent_id"=>$menu_id]);
        if(!empty($menu_count)){
            return $this->response(0,"该栏目下存在子栏目，不允许删除!");
        }
        return $this->response(1,"");
    }

    //栏目设置-编辑
    public function actionGetMenu(){
        $menu_id=I("menu_id");
        $MenuService=new MenuService();
        $menu=$MenuService->getOneById($menu_id);
        return $this->response(1,"",$menu);
    }

    //栏目设置-删除
    public function actionDeleteMenu(){
        $menu_id=I("menu_id");
        $MenuService=new MenuService();
        $MenuService->delete($menu_id);
        return $this->response(1,"");
    }

    //栏目设置-保存
    public function actionSaveMenu(){
        $menu=I("data");
        $menu=json_decode($menu,true);
        if(empty($menu)){
            return $this->response(0);
        }
        $MenuService=new MenuService();
        if(empty($menu["objectId"])){
            $menu_compare=$MenuService->getOne(["menu_name"=>$menu["menu_name"]]);
            if(!empty($menu_compare)){
                return $this->response(0,$menu["menu_name"]."已被使用，请修改！");
            }
            $MenuService->add($menu);
        }
        else{
            $menu_compare=$MenuService->getOne(["menu_name"=>$menu["menu_name"]]);
            if(!empty($menu_compare)&&$menu_compare["objectId"]!=$menu["objectId"]){
                return $this->response(0,$menu["menu_name"]."已被使用，请修改！");
            }
            $MenuService->update($menu);
        }
        return $this->response(1);
    }

    public function actionContentList()
    {
        $page=I("page",0);
        $MenuService=new MenuService();
        $menu_list=$MenuService->getAll([],["menu_sort asc"]);
        $children_menu_list=$root_meun_list=[];
        foreach($menu_list as $menu){
            if(!empty($menu["parent_id"])){
                $children_menu_list[$menu["parent_id"]][]=$menu;
            }
            else{
                $root_meun_list[]=$menu;
            }
        }

        $menu_id=I("menu_id");
        if(empty($menu_id)){
            $menu_id=current($root_meun_list)["objectId"];
        }
        $cur_menu=$MenuService->getOneById($menu_id);

        $ContentService=new ContentService();
        $right_content_list=$ContentService->get_data_for_show($cur_menu,$page,12);//右侧显示内容

        \Yii::$app->view->params['left_menu_active_id']=$cur_menu["objectId"];
        \Yii::$app->view->params['top_menu_active_id']="1";
        $cur_url=\Yii::$app->request->getHostInfo().\Yii::$app->request->url;
        $data=[
            "right_content_list"=>$right_content_list,
            "root_menu_list"=>$root_meun_list,
            "children_menu_list"=>$children_menu_list,
            "output"=>[
                "cur_menu"=>$cur_menu,
                "cur_url"=>$cur_url,
            ],
        ];
        return $this->render('content-list',$data);
    }

    public function actionContentEdit()
    {
        $mode=I("mode",1);
        $content_id=I("content_id",0);
        $MenuService=new MenuService();
        $menu_list=$MenuService->getAll([],["menu_sort asc"]);
        $children_menu_list=$root_meun_list=[];
        foreach($menu_list as $menu){
            if(!empty($menu["parent_id"])){
                $children_menu_list[$menu["parent_id"]][]=$menu;
            }
            else{
                $root_meun_list[]=$menu;
            }
        }

        $data=[
            "root_menu_list"=>$root_meun_list,
            "children_menu_list"=>$children_menu_list,
            "created_by"=>"",
            "menu_id"=>"",
            "title"=>"",
            "img_url"=>"",
            "detail_object_id"=>"",
            "detail"=>"",
            "is_top"=>false,
        ];

        if($mode==1){
            $userservice=\myVendor\Factorys::getUserService();
            $login_user_name=$userservice->getLoginUser();
            $data["created_by"]=$login_user_name;
            $data["menu_id"]=I("menu_id",0);
        }
        elseif($mode==2&&!empty($content_id)){
            $ContentService=new ContentService();
            $content=$ContentService->getOneById($content_id);
            $ContentDetailService=new ContentDetailService();
            $content_detail=$ContentDetailService->getOne(["content_id"=>$content_id]);
            $data["created_by"]=$content["createdBy"];
            $data["menu_id"]=$content["menu_id"];
            $data["title"]=$content["title"];
            $data["image_url"]=$content["image_url"];
            $data["is_top"]=$content["is_top"];
            $data["detail_object_id"]=$content_detail["objectId"];
            $data["detail"]=$content_detail["detail"];
        }
        \Yii::$app->view->params['top_menu_active_id']="1";
        return $this->render('content-edit',$data);
    }

    public function actionUpImage(){
        try{
            $uploader=\myVendor\Factorys::getUploder();
            $result=$uploader->uploadImage('attachmentUploader');
            $data =["success"=>$result["success"], "message"=>$result["message"],"data"=>$result["data"]];
            echo json_encode($data);
        }
        catch(\Exception $ex){
            $data =["success"=>0, "message"=>"","data"=>[]];
            echo json_encode($data);
        }
    }

    public function actionUpUeImage(){
        try{
            $uploader=\myVendor\Factorys::getUploder();
            $result=$uploader->uploadImage("upfile");
            if(!empty($result["success"])){
                $data =["originalName"=>"", "name"=>"","url"=>$result["data"]["url"],"size"=>0,"type"=>"","state"=>"SUCCESS"];
            }
            else{
                $data =["originalName"=>"", "name"=>"","url"=>"","size"=>0,"type"=>"","state"=>"FAIL","message"=>$result["message"]];
            }
            echo json_encode($data);
        }
        catch(\Exception $ex){
            $data =["originalName"=>"", "name"=>"","url"=>"","size"=>0,"type"=>"","state"=>"FAIL","message"=>$ex->getMessage()];
            echo json_encode($data);
        }
    }

    public function actionSaveContent(){
        $content=I("data");
        $mode=I("mode",1);
        $content_id=I("content_id",0);
        $content=json_decode($content,true);
        if(empty($content)){
            return $this->response(0);
        }
        $ContentService=new ContentService();
        $ContentDetailService=new ContentDetailService();
        $content["detail"]=\yii\helpers\HtmlPurifier::process($content["detail"], [
            'Attr.EnableID' => true,
        ]);
        if($mode==1){
            $content_id=$ContentService->add(["title"=>$content["title"],"menu_id"=>$content["menu_id"],"image_url"=>$content["image_url"],"createdBy"=>$content["createdBy"],"is_top"=>$content["is_top"],"pre_detail"=>$content["pre_detail"],"status"=>0,"hits"=>1]);
            $ContentDetailService->add(["content_id"=>$content_id,"detail"=>$content["detail"]]);
        }
        elseif($mode==2&&!empty($content_id)){
            $content["objectId"]=$content_id;
            $ContentService->update($content);
            $ContentDetailService->update(["objectId"=>$content["detail_object_id"],"detail"=>$content["detail"]]);
        }
        return $this->response(1);
    }

    public function actionDeleteContent(){
        $content_id=I("content_id");
        $ContentService=new ContentService();
        $ContentService->delete($content_id);
        $ContentDetailService=new ContentDetailService();
        $content_detail=$ContentDetailService->getOne(["content_id"=>$content_id]);
        $ContentDetailService->delete($content_detail["objectId"]);
        return $this->response(1);
    }

    public function actionParam(){
        $ParamService=new ParamService();
        $data=$ParamService->getBaseInfo();
        \Yii::$app->view->params['top_menu_active_id']="2";
        return $this->render('param',$data);
    }

    public function actionSaveParam(){
        $data_list=I("data_list");
        $data_list=json_decode($data_list,true);
        if(empty($data_list)){
            return $this->response(0);
        }
        $ParamService=new ParamService();
        foreach($data_list as $data){
            $obj=$ParamService->getOne(["keyname"=>$data["keyname"]]);
            if(!empty($obj)){
                $ParamService->update(["objectId"=>$obj["objectId"],"keyname"=>$data["keyname"],"keyvalue"=>$data["keyvalue"]]);
            }
            else{
                $ParamService->add(["keyname"=>$data["keyname"],"keyvalue"=>$data["keyvalue"]]);
            }
        }
        return $this->response(1);
    }

    public function actionCompany(){
        $ParamService=new ParamService();
        $data=$ParamService->getAll();
        $data= \yii\helpers\ArrayHelper::index($data, 'keyname');
        \Yii::$app->view->params['top_menu_active_id']="3";
        return $this->render('company',$data);
    }

    public function actionSaveCompany(){
        $data_list=I("data_list");
        $data_list=json_decode($data_list,true);
        if(empty($data_list)){
            return $this->response(0);
        }
        $ParamService=new ParamService();

        $key_name_list=["companyname","companyaddr","companytel","companyemail","companyperson","companyqrcode"];
        foreach($key_name_list as $key_name){
            $obj=$ParamService->getOne(["keyname"=>$key_name]);
            if(!empty($obj)){
                $ParamService->delete($obj["objectId"]);
            }
        }

        foreach($data_list as $data){
            $obj=$ParamService->getOne(["keyname"=>$data["keyname"]]);
            if(!empty($obj)){
                $ParamService->update(["objectId"=>$obj["objectId"],"keyname"=>$data["keyname"],"keyvalue"=>$data["keyvalue"]]);
            }
            else{
                $ParamService->add(["keyname"=>$data["keyname"],"keyvalue"=>$data["keyvalue"]]);
            }
        }
        return $this->response(1);
    }
}